/**
 * Created by ZFL on 2017/11/15.
 */
var wait = 60;//短信时间
var _false = false;//验证码是否在发送中
function appInit(){
    PageInit()
}
function PageInit() {

    if($.getPara("recommendFrom")!=""&& $.getPara('shareUserId')!=''){

        userInfo();
        $('#send-code-id').bind("click",function () {
            isRegister()

        });
    }
}

//注册 判断
function register() {
    if($(".tel input").val()==""){
        YDUI.dialog.toast('请输入手机号码', 1000);
    }else if($(".tel input").val().length!=11){
        YDUI.dialog.toast('手机格式有误', 1000);
    }else if($(".pwd-code input").val()==""){
        YDUI.dialog.toast('请输入验证码', 1000);
    }else if($(".pwd input").val()==""){
        YDUI.dialog.toast('请输入密码', 1000);
    }
    else{
        userReg();
    }
}

//判断手机号是否已注册
function isRegister(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.userName = $(".tel input").val();
            return obj;
        },
        visibleTime: 500,
        approve: false,
        api: "/yg_baijiaapp/user/MobileIsRegister",
        success:function(res){
            if(res.code==10000){
                if(!_false){
                    SendCode(); //发送验证码
                }
            }else{
                YDUI.dialog.toast('该手机号已注册',1000)
            }
        }

    })
}

//注册
function userReg() {
    if($(".pwd input").val() == ""){
        YDUI.dialog.toast("请输入密码",1000);
    }else if($(".pwd input").val().length<6){
        YDUI.dialog.toast("密码长度不能小于6位",1000);
    }else{
        var obj = {};
        //if($.getStorage('xm_uData')){
        if($.getStorage('xm_uData')){
            obj.nickName = JSON.parse($.getStorage('xm_uData')).nickName;
            obj.picture =JSON.parse($.getStorage('xm_uData')).picture;
            obj.sex = parseInt(JSON.parse($.getStorage('xm_uData')).sex);
        }else{
            obj.nickName = "败家";
            obj.picture ="http://h5.hm2019.com/b2c/images/logo_80.png";
            obj.sex =2;
        }
        obj.code=$(".pwd-code input").val();
        obj.userName = $(".tel input").val();
        obj.password=''+hex_md5($(".pwd input").val())+'';
        obj.recommendFrom =$.getPara("recommendFrom"); //推荐人ID

        $.ajaxHandler({
            model: function () {
                console.log(obj)
                return obj;
            },
            visibleTime: 500,
            approve: false,
            api: "/yg_baijiaapp/user/recommendUserRegister",
            success: function (result) {
                if (result.code == 10000){
                    var data=result.data;
                    //var url=window.location.href;
                    //window.localStorage.setItem('url',url);
                    $.setStorage('recommended',0);
                    YDUI.dialog.toast('注册成功', 'success', 1000);
                    saveInfo(data);
                }else if(result.code == 29901){
                    YDUI.dialog.toast('无效参数', 'success', 1000);
                }else if(result.code == 20101|| result.code == 20158){
                    YDUI.dialog.toast('用户已注册', 1000);
                    window.location.href='../app/downloadApp.html';


                } else if(result.code == 20109){
                    YDUI.dialog.toast('验证码不对', 'error', 1000);
                }
                else {
                    YDUI.dialog.toast(result.message, 1000);
                }
            }
        });
    }

}



//发送验证码
function SendCode() {
    if ($(".tel input").val() == "") {
        YDUI.dialog.toast('请输入手机号码', 1000);
    } else if ($(".tel input").val().length != 11) {
        YDUI.dialog.toast('手机号码格式不正确', 1000);
    } else {
        $.ajaxHandler({
                model: function () {
                    var obj = {};
                    obj.mobile = $('.tel input').val();
                    obj.type = 1;
                    return obj;
                },
                visibleTime: 100,
                dataType: "json",
                api: 'yg_baijiaapp/CheckCode/sendVerificationCode',
                success: function (result) {
                    console.log(result);
                    if (result.code == 10000) {
                        _false  = true;
                        YDUI.dialog.toast('短信发送成功', 'success', 1000);
                        SendTime("#send-code-id");
                    } else if (result.code == 20112) {
                        YDUI.dialog.toast('短信发送失败', 'error', 1000);
                    } else if (result.code == 20000) {
                        YDUI.dialog.toast('请求失败', 'error', 1000);
                    } else if (result.code == 29901) {
                        YDUI.dialog.toast('无效参数', 'error', 1000);
                    } else {
                        YDUI.dialog.toast('短信发送失败', 'error', 1000);
                    }

                }
            }
        );
    }
}

function SendTime(parameter){
    var Send = $(""+parameter+"");
    if (wait == 0) {
        Send.html("|点击获取验证码");
        wait = 60;
        _false  = false;
    } else {
        Send.html("重新发送(" + wait + ")s");
        wait--;
        setTimeout(function() {
            SendTime(parameter);
        },1000);
    }
}

//根据用户id查询用户信息
function userInfo() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.userId =$.getPara('shareUserId');
            console.log(obj)
            return obj;
        },
        visibleTime: 500,
        approve: false,
        api: "yg_baijiaapp/user/getUserEntityByIdNoPassword",
        success: function (result) {
            //console.log(result);
            if (result.code == 10000) {
                var data = result.data;
                var cdata=data.userInfo;
                //persionId=cdata.persionId;//记录推荐人身份ID
                $(".us-invite span").text('来自'+data.nickName+'的邀请函');
                if(data.picture && data.picture==""){
                    $(".info-1 img").attr("src","../src/images/logo_80.png");
                }else{
                    if(data.picture.toString().indexOf("http")>-1){
                        $(".info-1 img").attr("src",data.picture);
                    }else {
                        $(".info-1 img").attr("src",$.imgApi()+''+data.picture);
                    }
                }
                document.title=data.nickName+'邀请你注册败家商城';
            }else {
                YDUI.dialog.toast('异常',1000)
            }
        }
    });
}
